package com.hejian.android.a72ggametools.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.activity.GiftDetailActivity;
import com.hejian.android.a72ggametools.adapter.GiftPageAdapter;
import com.hejian.android.a72ggametools.adapter.HandGiftAdapter;
import com.hejian.android.a72ggametools.bean.GiftInfo;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.net.HttpUtils;
import com.hejian.android.a72ggametools.utils.JsonParse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/8.
 */
public class GiftFragment extends Fragment {
    private View view;
    private Button handGameBtn;
    private Button pageGameBtn;
    private ViewPager giftViewPager;
    private ArrayList<View> viewList;
    private GiftPageAdapter giftViewAdapter;
    private LinearLayout giftBtnLine;
    private View view2;
    private View view1;
    private List<GiftInfo> giftInfoList;
    private List<GiftInfo> pageGiftInfoList;
    private HandGiftAdapter handGiftAdapter;
    private HandGiftAdapter pageGiftAdapter;
    private int handPage = 0;
    private int pagePage = 0;
    private Intent intent;
    private ListView handGiftListView;
    private ListView pageGiftListView;
    private LinearLayout handUnfound;
    private LinearLayout pageUnfound;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_gift, null);
        initData();
        initView();
        setAdapter();
        setListener();
        return view;
    }

    private void setListener() {
        setGiftViewPagerListener();
        setGiftBtnListener();
        setListItemListener();
    }

    private void setListItemListener() {
        handGiftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent = new Intent(getActivity(), GiftDetailActivity.class);
                String handGiftId = giftInfoList.get(position).getId();
                intent.putExtra("id", handGiftId);
                startActivity(intent);
            }
        });
        pageGiftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent = new Intent(getActivity(), GiftDetailActivity.class);
                String pageGiftId = pageGiftInfoList.get(position).getId();
                intent.putExtra("id", pageGiftId);
                startActivity(intent);
            }
        });
    }

    private void setGiftBtnListener() {
        setBtnListener(0);
        setBtnListener(1);


    }

    private void setBtnListener(final int position) {
        giftBtnLine.getChildAt(position).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentItem(position);
                giftViewPager.setCurrentItem(position);
            }
        });
    }

    private void setGiftViewPagerListener() {
        giftViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCurrentItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setCurrentItem(int position) {
        switch (position) {
            case 0:
                handGameBtn.setBackgroundResource(R.drawable.half_left_shape_blue);
                handGameBtn.setTextColor(Color.WHITE);
                pageGameBtn.setBackgroundResource(R.drawable.half_right_shape_white);
                pageGameBtn.setTextColor(Color.BLUE);
                break;
            case 1:
                handGameBtn.setBackgroundResource(R.drawable.half_left_shape_white);
                handGameBtn.setTextColor(Color.BLUE);
                pageGameBtn.setBackgroundResource(R.drawable.half_right_shape_blue);
                pageGameBtn.setTextColor(Color.WHITE);
                break;
        }
    }

    private void setAdapter() {
        giftViewAdapter = new GiftPageAdapter(viewList);
        giftViewPager.setAdapter(giftViewAdapter);


    }

    private void initData() {
        initViewListData();
        loadNextHandPage();
        loadNextPagePage();

    }

    private void loadNextHandPage() {
//        handPage++;
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadHandOnePage(handPage);

            }
        }).start();
    }

    private void loadNextPagePage() {
//        pagePage++;
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadPageOnePage(pagePage);

            }
        }).start();
    }

    private void loadPageOnePage(int pagePage) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("platform", "2");
        map.put("gifttype", "2");
        map.put("page", String.valueOf(pagePage));
        String buffer = HttpUtils.doPostRequest(Constant.GIFT_LIST_URL, map);
        List<GiftInfo> list = JsonParse.parseHandGiftJson(buffer);
        Message msg = Message.obtain();
        msg.what = 2;
        msg.obj = list;
        pageGiftInfoList = list;
        handler.sendMessage(msg);
    }

    private void loadHandOnePage(int handPage) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("platform", "2");
        map.put("gifttype", "1");
        map.put("page", String.valueOf(handPage));
        String buffer = HttpUtils.doPostRequest(Constant.GIFT_LIST_URL, map);
        Log.i("demo", "loadOnePage: " + buffer);
        List<GiftInfo> list = JsonParse.parseHandGiftJson(buffer);
        Message msg = Message.obtain();
        msg.what = 1;
        msg.obj = list;
        giftInfoList = list;
        handler.sendMessage(msg);

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                if (giftInfoList != null) {
                    handGiftListView.setVisibility(View.VISIBLE);
                    handUnfound.setVisibility(View.INVISIBLE);
                    handGiftAdapter = new HandGiftAdapter(giftInfoList, getActivity());
                    handGiftListView.setAdapter(handGiftAdapter);

                } else {
                    handGiftListView.setVisibility(View.INVISIBLE);
                    handUnfound.setVisibility(View.VISIBLE);
                }

            } else if (msg.what == 2) {
                pageUnfound.setVisibility(View.VISIBLE);
                pageGiftListView.setVisibility(View.INVISIBLE);
//                if (pageGiftInfoList != null) {
//                    pageGiftListView.setVisibility(View.VISIBLE);
//                    pageUnfound.setVisibility(View.INVISIBLE);
//                    pageGiftAdapter = new HandGiftAdapter(pageGiftInfoList, getActivity());
//                    pageGiftListView.setAdapter(pageGiftAdapter);
//                } else {
//                    pageGiftListView.setVisibility(View.INVISIBLE);
//                    pageUnfound.setVisibility(View.VISIBLE);
//                }
            } else {
                loadNextHandPage();
                loadNextPagePage();

            }
        }
    };


    private void initViewListData() {
        viewList = new ArrayList<View>();
        view1 = LayoutInflater.from(getContext()).inflate(R.layout.gift_viewpager, null);
        view2 = LayoutInflater.from(getContext()).inflate(R.layout.gift_viewpager, null);
        viewList.add(view1);
        viewList.add(view2);

    }

    private void initView() {
        giftBtnLine = (LinearLayout) view.findViewById(R.id.line_gift_btn);
        giftViewPager = (ViewPager) view.findViewById(R.id.gift_viewpager);
        giftViewPager.setCurrentItem(0);
        handGameBtn = (Button) giftBtnLine.getChildAt(0);
        pageGameBtn = (Button) giftBtnLine.getChildAt(1);
        handGiftListView = (ListView) view1.findViewById(R.id.gift_listview);
        pageGiftListView = (ListView) view2.findViewById(R.id.gift_listview);
        handUnfound = (LinearLayout) view1.findViewById(R.id.gift_unfound);
        pageUnfound = (LinearLayout) view2.findViewById(R.id.gift_unfound);


    }

}
